/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT,
 * THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package lineage.ui.function.window.secretchat;

import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;

import javax.swing.JScrollPane;

import lineage.ui.customize.L1CustomizeTextArea;

/**
 * 带滚动条的多行文本框-密聊窗口输入栏.
 * 
 * @version 2014年2月27日下午6:56:58
 * @author jrwz
 */
public final class L1BoxInput extends L1CustomizeTextArea {
    private static final long serialVersionUID = 5077347376615322274L;

    /**
     * 静态初始化器，由JVM来保证线程安全.
     */
    private static class Holder {
        static L1BoxInput instance = new L1BoxInput();
    }

    /**
     * 取得该类的实例.
     */
    public static L1BoxInput getInstance() {
        return Holder.instance;
    }

    private L1BoxInput() {
        // 要设置的部分
        _x = 9;
        _y = 355;
        _width = 458;
        _height = 53;
        presetText = "按下（Enter+Ctrl）键发送";
        // end

        init(true);
        // setOpaque(true); // 设置不透明度
    }

    @Override
    public JScrollPane addJSP() {
        super.addJSP();
        jsp.setBorder(null); // 去边框
        jsp.setBounds(_x - 1, _y - 1, _width + 4, _height + 3);
        // jsp.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); // 横向滚动条永远不出现
        return jsp;
    }

    /**
     * 输出信息.
     */
    private void sendMessage() {
        L1WindowSecretChat.getInstance().appChatText(true, getText());
        setText("");
    }

    // 按下某个键时调用.
    @Override
    public void keyPressed(KeyEvent e) {
        switch (e.getKeyCode()) {
        case KeyEvent.VK_ENTER: // 按下回车+Ctrl键
            if (e.isControlDown()) {
                sendMessage();
            }
            break;
        default:
            break;
        }
    }

    // 得到焦点
    @Override
    public void focusGained(FocusEvent e) {
        if (getText().equalsIgnoreCase(presetText)) {
            setText(""); // 清空
        }
    }

    // 失去焦点
    @Override
    public void focusLost(FocusEvent e) {
        if (getText().equalsIgnoreCase("")) {
            setText(presetText); // 写入预设字符串
        }
    }
}
